package com.yls.except.util.kuaidiniao;

import com.yls.except.util.Base64;
import com.yls.except.util.ConstantUtil;
import com.yls.except.util.GeneralResult;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map; 

/**
 *
 * 快递鸟单号识别接口
 *
 * @技术QQ群: 456320272
 * @copyright: 深圳市快金数据技术服务有限公司
 *
 * 
 * ID和Key请到官网申请：http://www.kdniao.com/ServiceApply.aspx
 */

public class KdApiOrderDistinguish {
	
	//DEMO
	public static void main(String[] args) {
		KdApiOrderDistinguish api = new KdApiOrderDistinguish();
		try {
			String result = api.getOrderTracesByJson("811279018204");
			System.out.print(result);
			Map<String, Object> map = GeneralResult.jsonStrToMap(result);
			System.out.println(map.get("EBusinessID"));
			System.out.println(map.get("Success"));
			System.out.println(map.get("LogisticCode"));
			System.out.println(map.get("Shippers"));
			JSONArray jsonArray = (JSONArray) map.get("Shippers");
			System.out.println(((JSONObject)jsonArray.get(0)).get("ShipperCode"));
			System.out.println(((JSONObject)jsonArray.get(0)).get("ShipperName"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//请求url
	private static String REQURL="http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx";
 
	/**
     * Json方式 单号识别
	 * @throws Exception 
     */
	public static String getOrderTracesByJson(String expNo){
		String requestData= "{'LogisticCode':'" + expNo + "'}";
		
		Map<String, String> params = new HashMap<String, String>();
		try {
			params.put("RequestData", KdApiCreateOrderDemo.urlEncoder(requestData, "UTF-8"));
			params.put("EBusinessID", ConstantUtil.EBUSINESSID);
			params.put("RequestType", "2002");
			String dataSign=KdApiCreateOrderDemo.encrypt(requestData, ConstantUtil.APPKEY, "UTF-8");
			params.put("DataSign", KdApiCreateOrderDemo.urlEncoder(dataSign, "UTF-8"));
			params.put("DataType", "2");

			String result=KdApiCreateOrderDemo.sendPost(REQURL, params);
			return result;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
		//根据公司业务处理返回的信息......
	}

}
